//Main File
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "oplib.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
const char* Oprand[2]={"-f","-l"};
const char*	FileType[2]={".txt",".c"};
int main(int argc, char *argv[]) {
	FILE* file;
	int i;
	int FileMin=0,FileMax=0;
	for(i=1;i<argc;i++){
		OpndChk(argv[i],Oprand[0],&FileMin,i);
	}

	if(FileMin==0){
	HintDisp("Command -f Missing,Please Input Again.");
	ErrDisp("Your Commands are NOT ALL CORRECT,Try Again.");
	return 0;
	}else if(FileMin==argc){
	HintDisp("No File to Analyse");
	ErrDisp("Your Commands are NOT ALL CORRECT,Try Again.");		
	return 0;
	}else if(FileMin>2 && (stricmp(argv[1],Oprand[1])!=0)){
	HintDisp("Syntax Problem");
	ErrDisp("Your Commands are NOT ALL CORRECT,Try Again.");
	return 0;
	}
	FileMax=argc;

	printf("\nStatistical Data As Follows\n");
	printf("------------------------------\n");
	for(i=FileMin;i<FileMax;i++){//Each File Procedure
		printf("File %d\tResult:\n",i-FileMin+1);
		file=fopen(argv[i],"r");
		if(file==NULL){
			ErrDisp("File Do Not Exist,Please Input Again.");
			printf("------------------------------\n");
			continue;//Goto Next File Procedure		
		}	
		if(strcmp(argv[1],Oprand[0])==0 && FileTypeChk(argv[i],FileType[0])!=0){
			HintDisp("Surrfix Failure--Check Your File Surrfix");
			HintDisp("-f Command Single-Use Calculate .txt Only");
			ErrDisp("FileType Error!");
			continue;
		}else if(strcmp(argv[1],Oprand[1])==0 && FileTypeChk(argv[i],FileType[1])!=0){
			HintDisp("Surrfix Failure--Check Your File Surrfix");
			HintDisp("-c Command Calculate .c Only");
			ErrDisp("FileType Error!");
			continue;
		}
	char a;
	int CharCnt/*=-1*/,WordCnt/*=0*/,SentenceCnt/*=0*/,LineCnt/*=0*/;
		InitCntNum(&CharCnt,&WordCnt,&SentenceCnt,&LineCnt);
		do{
			a=fgetc(file);
			CharCnt++;
			WordChk(&a,&WordCnt);
			SentenceCnt+=PuctChk(&a);
			LineChk(&a,&LineCnt);	
		}while(a!=EOF);
	fclose(file);
//	WordCnt+=SentenceCnt;
	if(stricmp(argv[1],Oprand[1])!=0){
	ResDisp("Character Number",&CharCnt);
	ResDisp("     Word Number",&WordCnt);
	ResDisp(" Sentence Number",&SentenceCnt);
	}/* if(stricmp(argv[1],Oprand[1])==0)*/else{
	LineCnt--;
	ResDisp("     Line Number",&LineCnt);
	}
	printf("------------------------------\n");
	}
	return 0;
}
